function [x] = SMMmain(vec0)

load('SMM',...
    'p_a_init',...
    'power_p_a');

%close all %close figure windows
% p0_aux = abs(tanh(vec0(1)));
% p0_SMM = p0_init+(p0_aux^power_p0/p0_init^power_p0-1);
% mu_c_aux = vec0(2);
% mu_c_SMM = mu_c_init+(mu_c_aux^power_mu_c/mu_c_init^power_mu_c-1);
% 
% sig_eps_a_aux = exp( vec0(3));
% sig_eps_a_SMM = sig_eps_a_init+(sig_eps_a_aux^power_sig_eps_a/sig_eps_a_init^power_sig_eps_a-1);
% rho_a_aux = tanh(vec0(4));
% rho_a_SMM = rho_a_init+(rho_a_aux^power_rho_a/rho_a_init^power_rho_a-1);

%p0_SMM = abs(tanh(vec0(1)));
%mu_c_SMM = vec0(1);
%sig_c_SMM = vec0(2);
%sig_eps_a_SMM =exp( vec0(2));
p_a_SMM = abs(tanh(vec0(1)));
%--------------------------------------------------------------------
% Expanding the search area around 'phi' (transformation of phi_aux:)
%load('SMM','phi_init','power_phi');
%phi_aux = vec0(7);
%phi_SMM = phi_init+(phi_aux^power_phi/phi_init^power_phi-1);
%--------------------------------------------------------------------
fprintf(' \n');
fprintf(' \n');

disp('==========================================================')
disp('SIMULATED METHOD OF MOMENTS: PARAMETER VALUES IN ALGORITHM')
% %fprintf('p0           %4.6f (%4.6f) \n', p0_SMM, vec0(1));
% fprintf('mu_c         %4.6f (%4.6f)\n', mu_c_SMM, vec0(1));
% %fprintf('sig_c        %4.6f (%4.6f)\n', sig_c_SMM, vec0(2));
% fprintf('sig_eps_a0   %4.6f (%4.6f)\n', sig_eps_a_SMM, vec0(2));
fprintf('p_a0   %4.6f (%4.6f)\n',p_a_SMM, vec0(1));

disp('==========================================================')

save('SMM','p_a_SMM','-append')
%'rkappa_d_SMM','alfa_SMM','eta_SMM','-append')
% ---------------------------------------------------------
% check for bounds
% if p0_SMM > 1; % UB rho
%     p0_SMM=0.8;
%     disp('Parameter value out of permissable bounds')
%     disp('1')
%     
% elseif p0_SMM < 0
%     p0_SMM=0.1;
%     disp('Parameter value out of permissable bounds')
%     disp('2')
%     
% elseif mu_c_SMM < 0
%     mu_c_SMM=0.05;
%     disp('Parameter value out of permissable bounds')
%     disp('3')
% elseif mu_c_SMM > 0.4
%     mu_c_SMM=0.35;
%     disp('Parameter value out of permissable bounds')
%     disp('4')
% % elseif sig_c_SMM > 0.2;
% %     sig_c_SMM=0.15  ; 
% %     disp('Parameter value out of permissable bounds')
% %     disp('5')
% % elseif sig_c_SMM < 0;
% %     sig_c_SMM=0.05;   
% %     disp('Parameter value out of permissable bounds')
% %     disp('6')
% elseif sig_eps_a_SMM < 0;
%     sig_eps_a_SMM=0.02;    
%     disp('Parameter value out of permissable bounds')
%     disp('7')
% elseif sig_eps_a_SMM > 1;
%    sig_eps_a_SMM=0.05  
%     disp('Parameter value out of permissable bounds')
%     disp('8')
% 
% end
%     
%global par
disp('Initializing the system...')
%=====================================
% START OF COPY AND PASTE FROM 'MAIN' FILE:

main % set options such that it runs just like that!
%%=========================================================================
% End of copy-past of main file!

 % for sens_tours
%==========================================================================
%% 7. SMM Estimator
%cd output
%load('moments','meanb_mom','meang_mom','meanc_mom','autology','std_logy_mom',...
%    'meandeftfrequency','corrsovspready_mom','meankappa_mom','meany_mom')
%cd .. 
%load('SMM','target')
%load parameters

m_target = target;

m_model = [f2;...
    avfracup2;...
     med2;...
    interq2;...
       kur2  ];

m_scale=scale;

G = m_target - m_model;
% Weighting matrix W
size_weights = max(size(target));
scale = (ones(size_weights,1).*[4;1;1;1;1])./m_scale; 
% take probability of default with 'double importance'
W = eye(size_weights).*(ones(size_weights,1)*scale');
clear scale size_weights

dist_sqrd = G.*G;
W_vector = [W(1,1);W(2,2);W(3,3);W(4,4);W(5,5)];
dist_weighted = dist_sqrd.*W_vector;
% Calculate new SMM estimator:

x = G'*W*G;
save('SMM','x','dist_sqrd','W_vector','dist_weighted','-append')
% store convergence history
load('SMM','mu_c_SMM','sig_eps_a_SMM','p_a_SMM')
load('SMM','conv_SMM','xopt_SMM','conv_opt','SMM_count','tol_SMM')
% Keep track of number of rounds in SMM algorithm
SMM_count = SMM_count+1;
% Save outcome of this iteration step:
% a) The used parameters in 'theta2'
add_conv_SMM = [x,p_a_SMM];
conv_SMM = [conv_SMM;add_conv_SMM];
% Update 'best set of parameters'
if x < xopt_SMM % a) improvement? b) converged problem?
    %VFI_conv_SMM
    disp('IMPROVEMENT FOUND -- UPDATING THE BEST VALUE!')
    xopt_SMM = x;
    conv_opt = [x,0,0,p_a_SMM,0;... %  parameters
                m_model(1),m_model(2),m_model(3),m_model(4),m_model(5);...   % model moments, targeted
                target(1),target(2),target(3),target(4),target(5)];   % model moments, targeted
    %save 'best outcome'
    
    
    %copyfile('numeric.mat','SMM\best','f');
    %copyfile('parameters.mat','SMM\best','f');
    %copyfile('policies.mat','SMM\best','f');
    %copyfile('VFI.mat','SMM\best','f');
    %copyfile('PE.mat','SMM\best','f');
    %copyfile('calibration.txt','SMM\best','f'); 
    %cd output
    %    copyfile('statistics.txt','..\SMM\best','f'); 
    %cd ..
end

%% 8. OUTPUT/PRINT
fprintf('     \n');
disp('===================================================================================');
fprintf('MODEL SOLUTION (No.)      (%4.0f)  \n', SMM_count);
disp('Model moments           Sqrd dist.    Wght dist.    (target)    (weight)');
fprintf('1       %4.6f      %4.3f        %4.3f        (%4.4f)      (%4.3f)  \n',m_model(1), dist_sqrd(1), dist_weighted(1), m_target(1), W_vector(1));
fprintf('2       %4.6f      %4.3f        %4.3f        (%4.4f)      (%4.3f)   \n',  m_model(2),dist_sqrd(2), dist_weighted(2),m_target(2), W_vector(2));
fprintf('3       %4.6f      %4.3f        %4.3f        (%4.4f)      (%4.3f)   \n',  m_model(3),dist_sqrd(3), dist_weighted(3),m_target(3), W_vector(3));
fprintf('4       %4.6f      %4.3f        %4.3f        (%4.4f)      (%4.3f)   \n', m_model(4),dist_sqrd(4), dist_weighted(4), m_target(4), W_vector(4));
fprintf('5       %4.6f      %4.3f        %4.3f        (%4.4f)      (%4.3f)   \n',  m_model(5),dist_sqrd(5), dist_weighted(5),m_target(5), W_vector(5));
%fprintf('6       %4.6f      %4.3f        %4.3f        (%4.4f)      (%4.3f)   \n',  m_model(6),dist_sqrd(6), dist_weighted(6),m_target(6), W_vector(6));
%fprintf('7   %4.6f      %4.3f        %4.3f        (%4.4f)      (%4.3f)   \n', m_model(7),dist_sqrd(7), dist_weighted(7), m_target(7), W_vector(7));
disp('--------------------------------------------------------------------------------------------------------------------------------------------------');
disp('SIMULATED METHOD OF MOMENTS HISTORY:')
fprintf('       F(x)         p0          mu_c        sig_c        sig_eps_a   p_a \n');
disp(conv_SMM) 
disp('--------------------------------------------------------------------------------------------------------------------------------------------------');
disp('BEST SET OF PARAMETERS (RESULTS):')
fprintf('        F(x)         p0          mu_c        sig_c       sig_eps_a   p_a   \n');
disp(conv_opt(1,:))
fprintf('              1            2            3            4             5              6     \n');
fprintf('           (target)     (target)     (target)    (target)      (target)       (target)  \n');
disp(conv_opt(2:3,:))
disp('==================================================================================================================================');
% Save results
save('SMM','m_model','G','conv_SMM','xopt_SMM','conv_opt','SMM_count','-append')
% SMMprint
% copyfile('SMM.txt','SMM','f'); 
% copyfile('SMM.mat','SMM','f'); 
% dropbox:
%copyfile('SMM.txt','D:\Dropbox\DIW_sync\2.Dysfin\Matlab\remote-control\SMM','f');
%copyfile('SMM.mat','D:\Dropbox\DIW_sync\2.Dysfin\Matlab\remote-control\SMM','f');
% Check if tolerance for convergence reached!
% if x < tol_SMM
%     disp('*********************************')
%     disp('A SUITABLE CALIBRATION WAS FOUND!')
%     disp('*********************************')
%     return
% end

end